﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;
using MyScreenSaver;

using System.IO;

namespace MyScreenSaver
{
   
    public partial class SettingsForm : Form
    {
        static List <Word> myDictionary= new List<Word>();
       
        public SettingsForm()
        {
            InitializeComponent();
            dictionaryGrid.ColumnCount = 3;
            
                       
        }

        static int TimerInt = 3000; //по умолчанию
        
        private void okButton_Click(object sender, EventArgs e)
        {
            try
            {
                TimerInt = int.Parse(TimerTextBox.Text);
            }
            catch(Exception)
            {
                MessageBox.Show("Будет использован интервал по умолчанию");
            }

            ShowScreenSaver();
            
            
        }
        static void ShowScreenSaver()
        {
            foreach (Screen screen in Screen.AllScreens)
            {
                ScreenSaverForm screensaver = new ScreenSaverForm(screen.Bounds, myDictionary,TimerInt);
                screensaver.Show();
            }
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        

        private void addButton_Click(object sender, EventArgs e)
        {
            if (englishTextBox.Text != "" && russianTextBox.Text != "" && descriptionTextBox.Text != "")
            {
                Word word0 = new Word();
                word0.EnglishWord = englishTextBox.Text;
                word0.RussionWord = russianTextBox.Text;
                word0.Description = descriptionTextBox.Text;
                myDictionary.Add(word0);
                int iRows = dictionaryGrid.RowCount;
               
                dictionaryGrid.RowCount++;
                dictionaryGrid.Rows[iRows ].Cells[0].Value = word0.EnglishWord;
                dictionaryGrid.Rows[iRows ].Cells[1].Value = word0.RussionWord;
                dictionaryGrid.Rows[iRows ].Cells[2].Value = word0.Description;
               using (StreamWriter writer = new StreamWriter("dictionary.txt",true))
               {
                   writer.WriteLine(word0.EnglishWord + "*" + word0.RussionWord + "*" + word0.Description);
               }
                            }
            else
                MessageBox.Show("Слово не добавлено, введите значения во все поля");
        }

        private void SettingsForm_Load(object sender, EventArgs e)
        {
            using (StreamReader reader = new StreamReader("dictionary.txt"))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                   
                    string[] array = line.Split('*');
                    Word word0 = new Word();
                    word0.EnglishWord = array[0];
                    word0.RussionWord = array[1];
                    word0.Description = array[2];
                    myDictionary.Add(word0);
                    int iRows = dictionaryGrid.RowCount;
                    dictionaryGrid.RowCount++;
                    dictionaryGrid.Rows[iRows ].Cells[0].Value = word0.EnglishWord;
                    dictionaryGrid.Rows[iRows ].Cells[1].Value = word0.RussionWord;
                    dictionaryGrid.Rows[iRows ].Cells[2].Value = word0.Description;
                    
                }
            }

        }
                

    }
}
